package com.dbkj.axb_group;

import com.dbkj.common.model.AxbGroup;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.SqlPara;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Max
 * @Date: 2019/10/29 9:49
 */

public class AxbGroupService {

    public static final AxbGroup dao = new AxbGroup().dao();

    public Page<AxbGroup> paginate(Integer pageNumber, Integer pageSize,String name) {
        SqlPara sql = dao.getSqlPara("group.show3", Kv.by("name",name));
        Page<AxbGroup> page =  dao.paginate(pageNumber,pageSize,sql);
        List<AxbGroup> list = page.getList();

        for(int i=0;i<list.size();i++){
            if(StrKit.notBlank(list.get(i).getStr("a_relations"))){
                String[] ids = list.get(i).getStr("ids").split(",");
                String[] a_relations = list.get(i).getStr("a_relations").split(",");
                String[] levels = list.get(i).getStr("levels").split(",");
                for(int j=0;j<a_relations.length;j++){
                    Map map = new HashMap();
                    map.put("id",ids[j]);
                    map.put("aRelation",a_relations[j]);
                    map.put("level",levels[j]);
                    List list1 = list.get(i).getaRelationSetup();
                    if(null == list1){
                        list1 = new ArrayList();
                    }
                    list1.add(map);
                    list.get(i).setaRelationSetup(list1);
                }
            }

        }
        //System.out.println(list);
        return page;
    }

    public void delete(Integer id) {
        dao.deleteById(id);
    }

    public List<AxbGroup> listAll() {
        return dao.findAll();
    }

    public Integer getIdByName(String s) {
        AxbGroup g = dao.findFirst("select * from axb_group where group_name =?",s);
        if(null!=g){
            return g.getId();
        }else {
            return null;
        }
    }
}
